package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.IDocketLogDetailView;

import java.lang.reflect.Type;

public class DocketLogDetailPresenter extends BasePresenter<IDocketLogDetailView> {

    private DocketModel docketModel = new DocketModel();

    public void getData(String docketId,boolean isApi) {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT = new TypeToken<ApiResultData<DocketDto>>() {
            }.getType();
            if (isApi) {
                logDetail(docketId,typeofT);
            }else {
                logDetailLocal(docketId,typeofT);
            }

        }
    }

    private void logDetailLocal(String docketId, Type typeofT) {
        docketModel.logDetailLocal(docketId,new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetDataSuccess((ApiResultData<DocketDto>) aipResult,false);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetDataFail(e);
                }
            }
        }, typeofT);
    }

    private void logDetail(String docketId,Type typeofT) {
        docketModel.logDetail(docketId,new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetDataSuccess((ApiResultData<DocketDto>) aipResult,true);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetDataFail(e);
                }
            }
        }, typeofT);
    }
}
